var async = require('async')

module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
}

var handler = Handler.prototype
var local = {}

//获取成员列表
handler.getClubList = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!!uid){
		var targetUid = msg.uid
		if(!targetUid || typeof(targetUid) !== "number"){
			next(null,{flag : false})
			return
		}
		//获取成员列表
		self.app.rpc.db.club.getClubList(session,targetUid,function(data) {
			next(null,{"flag" : true,"data" : data})
		})
	}else{
		next(null,{flag : false})
	}
}


//添加俱乐部成员
handler.addClubPlayer = function(msg,session,next) {
	if(!msg.playerId || typeof(msg.playerId) != "number"){
		next(null,{"flag" : false,"result" : "参数错误"})
		return
	}
	var self = this
	var uid = session.get("uid")
	if(!!uid){
		//添加俱乐部成员
		self.app.rpc.db.club.addClubPlayer(session,uid,msg.playerId,function(data) {
			next(null,{"flag" : (data ? true : false),"data" : data})
		})
	}else{
		next(null,{flag : false})
	}	
}



//删除俱乐部成员
handler.removeClubPlayer = function(msg,session,next) {
	if(!msg.playerId || typeof(msg.playerId) != "number"){
		next(null,{"flag" : false,"result" : "参数错误"})
		return
	}
	var self = this
	var uid = session.get("uid")
	if(!!uid){
		//删除俱乐部成员
		self.app.rpc.db.club.removePlayer(session,uid,msg.playerId,function(data) {
			next(null,{"flag" : data})
		})
	}else{
		next(null,{flag : false})
	}
}



//仅俱乐部成员加入开关
handler.clubSwitch = function(msg,session,next) {
	if(typeof(msg.flag) !== "boolean"){
		next(null,{"flag" : false,code : "参数错误"})
		return
	}
	var self = this
	var uid = session.get("uid")
	if(!!uid){
		//开启俱乐部开关
		self.app.rpc.db.club.clubSwitch(session,uid,msg.flag,function(data) {
			next(null,{"flag" : data})
		})
	}else{
		next(null,{flag : false})
	}
}

//申请加入俱乐部
handler.applyJoinClub = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	if(!msg.teaMasterUid){
		next(null,{flag : false})
		return
	}
	if(uid == msg.teaMasterUid){
		console.log("不能申请加入自己的俱乐部")
		next(null,{flag : false})
		return		
	}
	this.app.rpc.db.club.applyJoinClub(session,uid,msg.teaMasterUid,function(data) {
		next(null,{"flag" : data})
	})
}
//获取申请列表
handler.getApplyList = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.club.getApplyList(session,uid,function(data) {
		next(null,{"flag" : data})
	})
}
//同意申请
handler.agreeApply = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	if(!msg.playerId){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.club.agreeApply(session,uid,msg.playerId,function(data) {
		next(null,{"flag" : data})
	})
}
//拒绝申请
handler.rejectApply = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	if(!msg.playerId){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.club.rejectApply(session,uid,msg.playerId,function(data) {
		next(null,{"flag" : data})
	})
}